package org.jivesoftware.smackx;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.MessageEvent;

public class MessageEventManager
{
  private Connection con;
  private List<MessageEventNotificationListener> messageEventNotificationListeners = new ArrayList();
  private List<MessageEventRequestListener> messageEventRequestListeners = new ArrayList();
  private PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:event");
  private PacketListener packetListener;

  public MessageEventManager(Connection paramConnection)
  {
    this.con = paramConnection;
    init();
  }

  public static void addNotificationsRequests(Message paramMessage, boolean paramBoolean1, boolean paramBoolean2, boolean paramBoolean3, boolean paramBoolean4)
  {
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setOffline(paramBoolean1);
    localMessageEvent.setDelivered(paramBoolean2);
    localMessageEvent.setDisplayed(paramBoolean3);
    localMessageEvent.setComposing(paramBoolean4);
    paramMessage.addExtension(localMessageEvent);
  }

  // ERROR //
  private void fireMessageEventNotificationListeners(String paramString1, String paramString2, String paramString3)
  {
    // Byte code:
    //   0: aconst_null
    //   1: checkcast 87	[Lorg/jivesoftware/smackx/MessageEventNotificationListener;
    //   4: pop
    //   5: aload_0
    //   6: getfield 27	org/jivesoftware/smackx/MessageEventManager:messageEventNotificationListeners	Ljava/util/List;
    //   9: astore 5
    //   11: aload 5
    //   13: monitorenter
    //   14: aload_0
    //   15: getfield 27	org/jivesoftware/smackx/MessageEventManager:messageEventNotificationListeners	Ljava/util/List;
    //   18: invokeinterface 93 1 0
    //   23: anewarray 95	org/jivesoftware/smackx/MessageEventNotificationListener
    //   26: astore 7
    //   28: aload_0
    //   29: getfield 27	org/jivesoftware/smackx/MessageEventManager:messageEventNotificationListeners	Ljava/util/List;
    //   32: aload 7
    //   34: invokeinterface 99 2 0
    //   39: pop
    //   40: aload 5
    //   42: monitorexit
    //   43: iconst_2
    //   44: anewarray 101	java/lang/Class
    //   47: astore 12
    //   49: aload 12
    //   51: iconst_0
    //   52: ldc 103
    //   54: aastore
    //   55: aload 12
    //   57: iconst_1
    //   58: ldc 103
    //   60: aastore
    //   61: ldc 95
    //   63: aload_3
    //   64: aload 12
    //   66: invokevirtual 107	java/lang/Class:getDeclaredMethod	(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
    //   69: astore 13
    //   71: iconst_0
    //   72: istore 14
    //   74: aload 7
    //   76: arraylength
    //   77: istore 15
    //   79: iload 14
    //   81: iload 15
    //   83: if_icmplt +12 -> 95
    //   86: return
    //   87: astore 6
    //   89: aload 5
    //   91: monitorexit
    //   92: aload 6
    //   94: athrow
    //   95: aload 7
    //   97: iload 14
    //   99: aaload
    //   100: astore 16
    //   102: iconst_2
    //   103: anewarray 4	java/lang/Object
    //   106: astore 17
    //   108: aload 17
    //   110: iconst_0
    //   111: aload_1
    //   112: aastore
    //   113: aload 17
    //   115: iconst_1
    //   116: aload_2
    //   117: aastore
    //   118: aload 13
    //   120: aload 16
    //   122: aload 17
    //   124: invokevirtual 113	java/lang/reflect/Method:invoke	(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
    //   127: pop
    //   128: iinc 14 1
    //   131: goto -57 -> 74
    //   134: astore 11
    //   136: aload 11
    //   138: invokevirtual 116	java/lang/NoSuchMethodException:printStackTrace	()V
    //   141: goto -55 -> 86
    //   144: astore 10
    //   146: aload 10
    //   148: invokevirtual 117	java/lang/reflect/InvocationTargetException:printStackTrace	()V
    //   151: goto -65 -> 86
    //   154: astore 9
    //   156: aload 9
    //   158: invokevirtual 118	java/lang/IllegalAccessException:printStackTrace	()V
    //   161: goto -75 -> 86
    //
    // Exception table:
    //   from	to	target	type
    //   14	43	87	finally
    //   89	92	87	finally
    //   43	79	134	java/lang/NoSuchMethodException
    //   95	128	134	java/lang/NoSuchMethodException
    //   43	79	144	java/lang/reflect/InvocationTargetException
    //   95	128	144	java/lang/reflect/InvocationTargetException
    //   43	79	154	java/lang/IllegalAccessException
    //   95	128	154	java/lang/IllegalAccessException
  }

  // ERROR //
  private void fireMessageEventRequestListeners(String paramString1, String paramString2, String paramString3)
  {
    // Byte code:
    //   0: aconst_null
    //   1: checkcast 120	[Lorg/jivesoftware/smackx/MessageEventRequestListener;
    //   4: pop
    //   5: aload_0
    //   6: getfield 29	org/jivesoftware/smackx/MessageEventManager:messageEventRequestListeners	Ljava/util/List;
    //   9: astore 5
    //   11: aload 5
    //   13: monitorenter
    //   14: aload_0
    //   15: getfield 29	org/jivesoftware/smackx/MessageEventManager:messageEventRequestListeners	Ljava/util/List;
    //   18: invokeinterface 93 1 0
    //   23: anewarray 122	org/jivesoftware/smackx/MessageEventRequestListener
    //   26: astore 7
    //   28: aload_0
    //   29: getfield 29	org/jivesoftware/smackx/MessageEventManager:messageEventRequestListeners	Ljava/util/List;
    //   32: aload 7
    //   34: invokeinterface 99 2 0
    //   39: pop
    //   40: aload 5
    //   42: monitorexit
    //   43: iconst_3
    //   44: anewarray 101	java/lang/Class
    //   47: astore 12
    //   49: aload 12
    //   51: iconst_0
    //   52: ldc 103
    //   54: aastore
    //   55: aload 12
    //   57: iconst_1
    //   58: ldc 103
    //   60: aastore
    //   61: aload 12
    //   63: iconst_2
    //   64: ldc 2
    //   66: aastore
    //   67: ldc 122
    //   69: aload_3
    //   70: aload 12
    //   72: invokevirtual 107	java/lang/Class:getDeclaredMethod	(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;
    //   75: astore 13
    //   77: iconst_0
    //   78: istore 14
    //   80: aload 7
    //   82: arraylength
    //   83: istore 15
    //   85: iload 14
    //   87: iload 15
    //   89: if_icmplt +12 -> 101
    //   92: return
    //   93: astore 6
    //   95: aload 5
    //   97: monitorexit
    //   98: aload 6
    //   100: athrow
    //   101: aload 7
    //   103: iload 14
    //   105: aaload
    //   106: astore 16
    //   108: iconst_3
    //   109: anewarray 4	java/lang/Object
    //   112: astore 17
    //   114: aload 17
    //   116: iconst_0
    //   117: aload_1
    //   118: aastore
    //   119: aload 17
    //   121: iconst_1
    //   122: aload_2
    //   123: aastore
    //   124: aload 17
    //   126: iconst_2
    //   127: aload_0
    //   128: aastore
    //   129: aload 13
    //   131: aload 16
    //   133: aload 17
    //   135: invokevirtual 113	java/lang/reflect/Method:invoke	(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;
    //   138: pop
    //   139: iinc 14 1
    //   142: goto -62 -> 80
    //   145: astore 11
    //   147: aload 11
    //   149: invokevirtual 116	java/lang/NoSuchMethodException:printStackTrace	()V
    //   152: goto -60 -> 92
    //   155: astore 10
    //   157: aload 10
    //   159: invokevirtual 117	java/lang/reflect/InvocationTargetException:printStackTrace	()V
    //   162: goto -70 -> 92
    //   165: astore 9
    //   167: aload 9
    //   169: invokevirtual 118	java/lang/IllegalAccessException:printStackTrace	()V
    //   172: goto -80 -> 92
    //
    // Exception table:
    //   from	to	target	type
    //   14	43	93	finally
    //   95	98	93	finally
    //   43	85	145	java/lang/NoSuchMethodException
    //   101	139	145	java/lang/NoSuchMethodException
    //   43	85	155	java/lang/reflect/InvocationTargetException
    //   101	139	155	java/lang/reflect/InvocationTargetException
    //   43	85	165	java/lang/IllegalAccessException
    //   101	139	165	java/lang/IllegalAccessException
  }

  private void init()
  {
    this.packetListener = new PacketListener(this)
    {
      public void processPacket()
      {
        Message localMessage = (Message)paramPacket;
        MessageEvent localMessageEvent = (MessageEvent)localMessage.getExtension("x", "jabber:x:event");
        if (localMessageEvent.isMessageEventRequest())
        {
          Iterator localIterator2 = localMessageEvent.getEventTypes();
          while (true)
          {
            if (!(localIterator2.hasNext()))
              return;
            MessageEventManager.access$0(this.this$0, localMessage.getFrom(), localMessage.getPacketID(), ((String)localIterator2.next()).concat("NotificationRequested"));
          }
        }
        Iterator localIterator1 = localMessageEvent.getEventTypes();
        while (true)
        {
          if (!(localIterator1.hasNext()));
          MessageEventManager.access$1(this.this$0, localMessage.getFrom(), localMessageEvent.getPacketID(), ((String)localIterator1.next()).concat("Notification"));
        }
      }
    };
    this.con.addPacketListener(this.packetListener, this.packetFilter);
  }

  public void addMessageEventNotificationListener(MessageEventNotificationListener paramMessageEventNotificationListener)
  {
    synchronized (this.messageEventNotificationListeners)
    {
      if (!(this.messageEventNotificationListeners.contains(paramMessageEventNotificationListener)))
        this.messageEventNotificationListeners.add(paramMessageEventNotificationListener);
      return;
    }
  }

  public void addMessageEventRequestListener(MessageEventRequestListener paramMessageEventRequestListener)
  {
    synchronized (this.messageEventRequestListeners)
    {
      if (!(this.messageEventRequestListeners.contains(paramMessageEventRequestListener)))
        this.messageEventRequestListeners.add(paramMessageEventRequestListener);
      return;
    }
  }

  public void destroy()
  {
    if (this.con != null)
      this.con.removePacketListener(this.packetListener);
  }

  protected void finalize()
    throws Throwable
  {
    destroy();
    super.finalize();
  }

  public void removeMessageEventNotificationListener(MessageEventNotificationListener paramMessageEventNotificationListener)
  {
    synchronized (this.messageEventNotificationListeners)
    {
      this.messageEventNotificationListeners.remove(paramMessageEventNotificationListener);
      return;
    }
  }

  public void removeMessageEventRequestListener(MessageEventRequestListener paramMessageEventRequestListener)
  {
    synchronized (this.messageEventRequestListeners)
    {
      this.messageEventRequestListeners.remove(paramMessageEventRequestListener);
      return;
    }
  }

  public void sendCancelledNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setCancelled(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendComposingNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setComposing(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendDeliveredNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setDelivered(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }

  public void sendDisplayedNotification(String paramString1, String paramString2)
  {
    Message localMessage = new Message(paramString1);
    MessageEvent localMessageEvent = new MessageEvent();
    localMessageEvent.setDisplayed(true);
    localMessageEvent.setPacketID(paramString2);
    localMessage.addExtension(localMessageEvent);
    this.con.sendPacket(localMessage);
  }
}